<template>
  <div class="login-container">
    <a-card class="login-card">
      <h2>实践项目教学支持系统</h2>
      <a-form :model="form" @finish="handleSubmit">
        <a-form-item>
          <a-input v-model:value="form.username" placeholder="学号/工号">
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-radio-group v-model:value="form.role" class="role-select">
            <a-radio-button value="STUDENT">学生</a-radio-button>
            <a-radio-button value="TEACHER">教师</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item>
          <a-input-password v-model:value="form.password" placeholder="密码">
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
        <a-form-item>
          <div class="remember-forgot">
            <a-checkbox v-model:checked="form.remember">记住我</a-checkbox>
            <a @click="handleForgotPassword">忘记密码？</a>
          </div>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit" block :loading="loading">登录</a-button>
        </a-form-item>
        <div class="register-link">
          还没有账号？<a @click="handleRegister">立即注册</a>
        </div>
      </a-form>
      <div class="login-footer">
        <div class="footer-links">
          <a @click="handleHelp">帮助</a>
          <a @click="handlePrivacy">隐私条款</a>
        </div>
        <div class="copyright">
          Copyright© 2025 西安欧亚学院
        </div>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { useUserStore } from '@/stores/user'
import { login } from '@/api/auth.js'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)
const form = reactive({
  username: '',
  role: 'STUDENT',
  password: '',
  remember: false
})

const handleSubmit = async () => {
  if (!form.username || !form.password) {
    message.error('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    // 先清除旧的数据
    userStore.clearUserInfo()
    const resp = await login({
      username: form.username,
      role: form.role,
      password: form.password
    })
    userStore.setToken(resp.token)
    userStore.setUserRole(form.role)
    userStore.setUserName(form.username)
    userStore.setUserId(resp.userId)
    message.success('登录成功')
    try {
      router.replace('/home')
    } catch (navigationError) {
      console.error('导航错误:', navigationError)
      message.error('页面跳转失败，请重试')
    }
  } catch (error) {
    console.error('登录处理错误:', error)
    message.error('登录过程中发生错误')
    userStore.clearUserInfo()
  } finally {
    loading.value = false
  }
}

// 添加路由跳转方法
const handleForgotPassword = () => {
  console.log('跳转到忘记密码页面')
  router.push({ name: 'forgotPassword' })
}

const handleRegister = () => {
  console.log('跳转到注册页面')
  router.push({ name: 'register' })
}

const handleHelp = () => {
  console.log('跳转到帮助页面')
  router.push({ name: 'help' })
}

const handlePrivacy = () => {
  console.log('跳转到隐私条款页面')
  router.push({ name: 'privacy' })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: url('@/assets/login-bg.jpg') no-repeat center center;
  background-size: cover;
  padding-right: 120px;
  position: fixed;
  top: 0;
  left: 0;
}

.login-card {
  width: 320px;
  text-align: center;
  background: rgba(255, 255, 255, 0.9) !important;
  backdrop-filter: blur(10px);
  border: none !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

h2 {
  margin-bottom: 24px;
  color: rgba(0, 0, 0, 0.85);
}

.role-select {
  width: 100%;
  display: flex;
}

.role-select :deep(.ant-radio-button-wrapper) {
  flex: 1;
  text-align: center;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.remember-forgot a {
  color: rgba(0, 0, 0, 0.45);
}

.register-link {
  margin-top: 16px;
  text-align: center;
}

.login-footer {
  margin-top: 48px;
  text-align: center;
}

.footer-links {
  margin-bottom: 8px;
}

.footer-links a {
  margin: 0 8px;
  color: rgba(0, 0, 0, 0.45);
}

.copyright {
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
}
</style>
